function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function randomNumArr(start, end, num){
  let rangeArray = (start, end) => Array(end - start + 1).fill(0).map((v, i) => i + start);
  let range = rangeArray(start, end), random = [];
  while (random.length < num && range.length) {
    let i = Math.random() * range.length | 0
    random.push(range[i])
    range.splice(i, 1)  
  }  
  return random;
}

function imageUtil(e){
  var imageSize = {};
  var originalWidth = e.detail.width; // 图片原始宽度
  var originalHeight = e.detail.height; // 图片原始高度
  var originalScale  = originalHeight / originalWidth; // 原始宽高比

  console.log("originalWidth:" + originalWidth + "; originalHeight:" + originalHeight);

  // 获取屏幕宽高

  wx.getSystemInfo({
    success: function(res) {
      // success
      var windowWidth = res.windowWidth;
      var windowHeight = res.windowHeight;
      var windowScale = windowHeight / windowWidth; // 屏幕宽高比
      console.log("windowWidth:" + windowWidth + "; windowHeight:" + windowHeight);

      if(originalScale < windowScale){
          // 图片缩放后的宽为屏幕宽
          imageSize.imageWidth = windowWidth;
          imageSize.imageHeight = (windowWidth * originalHeight) / originalWidth;
      }else{
          // 图片缩放后的高为屏幕高
          imageSize.imageHeight = windowHeight;
          imageSize.imageWidth = (windowHeight * originalHeight) / originalHeight;
      }
    }
  })

  return imageSize;
}

var trim = function(str){return str.replace(/(^\s*)|(\s*$)/g, "");}
var checkPhoneNum = function(str){
  var regBox = {
      regMobile : /^0?1[3|4|5|8][0-9]\d{8}$/,//手机  
      regTel : /^0[\d]{2,3}-[\d]{7,8}$/  //电话  
   }
   str = trim(str);
   if(regBox.regMobile.test(str) || regBox.regTel.test(str)){
     return true;
   }
   return false;
} 

var generatePageUrl = function(link){
  var link = trim(link);
  if(link == "") return;
  var linkArr = link.split("?", 2);

  return linkArr[0] + linkArr[0] + (linkArr[1] ? "?" + linkArr[1] : '');
}

module.exports = {
  formatTime: formatTime,
  imageUtil: imageUtil,
  randomNumArr: randomNumArr,
  trim: trim,
  checkPhoneNum: checkPhoneNum,
  generatePageUrl: generatePageUrl
}
